﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Finance.IDal;
using Finance.Poco;
using Finance.Entity;

namespace Finance.Dal
{
    public class OrgDal : BaseDal, IOrgDal
    {
        public bool Save(int id, string name)
        {
            Org org = new Org();
            
            org.Id = id;
            org.Name = name;
            if (org.Id == 0)
            {
                org.Attach(Dos.ORM.EntityState.Added);
            }
            else
            {
                org.Attach(Dos.ORM.EntityState.Modified);
            }
            return dbSession.Save<Org>(org) > 0;
        }

        public bool Delete(int id)
        {
            return dbSession.Delete<Org>(id) > 0;
        }

        public IEnumerable<Entity.OrgEntity> List(string name, int pageIndex, int pageSize, out int total)
        {
            var sql = dbSession.From<Org>();
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql.Where(org => org.Name.Like(name));
            }
            total = sql.Count();

           return  sql.Page(pageSize, pageIndex).ToList().Select(AutoMapper.Mapper.Map<OrgEntity>).ToList();
        }

        public IEnumerable<Entity.OrgEntity> List()
        {
            var sql = dbSession.From<Org>();
           
            return sql.ToList().Select(AutoMapper.Mapper.Map<OrgEntity>).ToList();
        }
    }
}
